'''
* This is the projet for Brtc LlmOps Platform
* @Author Leon-liao <liaosiliang@alltman.com>
* @Description //TODO 
* @File: app_handler.py
* @Time: 2025/9/17
* @All Rights Reserve By Brtc
'''

from dataclasses import dataclass
from uuid import UUID

from flask import request, current_app
from flask_login import login_required, current_user
from injector import inject

from internal.entity.dataset_entity import RetrievalStrategy, RetrievalSource
from internal.schema.app_schema import CreateAppReq, GetAppResp, GetPublishHistoriesWithPageReq, \
    GetPublishHistoriesWithPageResp, FallbackHistoryToDraftReq, UpdateDebugConversationSummaryReq, DebugChatReq
from internal.service import AppService, RetrievalService
from pkg.paginator.paginator import PageModel
from pkg.response import validate_error_json, success_json, success_message
from pkg.response.response import compact_generate_response


@inject
@dataclass
class AppHandler:
    """应用控制器"""
    app_service: AppService
    retrieval_service:RetrievalService

    @login_required
    def create_app(self):
        """调用服务创建新的APP记录"""
        # 1.提取请求并校验
        req = CreateAppReq()
        if not req.validate():
            return validate_error_json(req.errors)

        # 2.调用服务创建应用信息
        app = self.app_service.create_app(req, current_user)

        # 3.返回创建成功响应提示
        return success_json({"id": app.id})

    @login_required
    def get_app(self, app_id: UUID):
        """获取指定的应用基础信息"""
        app = self.app_service.get_app(app_id, current_user)
        resp = GetAppResp()
        return success_json(resp.dump(app))


    @login_required
    def get_draft_app_config(self, app_id: UUID):
        """根据传递的应用id获取应用的最新草稿配置"""
        draft_config = self.app_service.get_draft_app_config(app_id, current_user)
        return success_json(draft_config)


    @login_required
    def update_draft_app_config(self, app_id: UUID):
        """根据传递的应用id+草稿配置更新应用的最新草稿配置"""
        # 1.获取草稿请求json数据
        draft_app_config = request.get_json(force=True, silent=True) or {}
        # 2.调用服务更新应用的草稿配置
        self.app_service.update_draft_app_config(app_id, draft_app_config, current_user)
        return success_message("更新应用草稿配置成功")


    @login_required
    def publish(self, app_id: UUID):
        """根据传递的应用id发布/更新特定的草稿配置信息"""
        self.app_service.publish_draft_app_config(app_id, current_user)
        return success_message("发布/更新应用配置成功")


    @login_required
    def get_publish_histories_with_page(self, app_id: UUID):
        """根据传递的应用id，获取应用发布历史列表"""
        # 1.获取请求数据并校验
        req = GetPublishHistoriesWithPageReq(request.args)
        if not req.validate():
            return validate_error_json(req.errors)

        # 2.调用服务获取分页列表数据
        app_config_versions, paginator = self.app_service.get_publish_histories_with_page(app_id, req, current_user)

        # 3.创建响应结构并返回
        resp = GetPublishHistoriesWithPageResp(many=True)

        return success_json(PageModel(list=resp.dump(app_config_versions), paginator=paginator))


    @login_required
    def cancel_publish(self, app_id: UUID):
        """根据传递的应用id，取消发布指定的应用配置信息"""
        self.app_service.cancel_publish_app_config(app_id, current_user)
        return success_message("取消发布应用配置成功")


    @login_required
    def fallback_history_to_draft(self, app_id: UUID):
        """根据传递的应用id+历史配置版本id，退回指定版本到草稿中"""
        # 1.提取数据并校验
        req = FallbackHistoryToDraftReq()
        if not req.validate():
            return validate_error_json(req.errors)
        # 2.调用服务回退指定版本到草稿
        self.app_service.fallback_history_to_draft(app_id, req.app_config_version_id.data, current_user)
        return success_message("回退历史配置至草稿成功")


    @login_required
    def get_debug_conversation_summary(self, app_id: UUID):
        """根据传递的应用id获取调试会话长期记忆"""
        summary = self.app_service.get_debug_conversation_summary(app_id, current_user)
        return success_json({"summary": summary})

    @login_required
    def update_debug_conversation_summary(self, app_id: UUID):
        """根据传递的应用id+摘要信息更新调试会话长期记忆"""
        # 1.提取数据并校验
        req = UpdateDebugConversationSummaryReq()
        if not req.validate():
            return validate_error_json(req.errors)
        # 2.调用服务更新调试会话长期记忆
        self.app_service.update_debug_conversation_summary(app_id, req.summary.data, current_user)
        return success_message("更新AI应用长期记忆成功")

    @login_required
    def ping(self):
        dataset_retrieval = self.retrieval_service.create_langchain_tool_from_serch(
            flask_app=current_app,
            dataset_ids=[UUID("dc0e7aa8-4c1d-480d-951d-3a310938af79")],
            account_id=current_user.id,
            retrieval_strategy=RetrievalStrategy.SEMANTIC,
            k = 10,
            score= 0.5,
            retrieval_source=RetrievalSource.DEBUGGER
        )
        print("工具名称", dataset_retrieval.name)
        print("工具描述", dataset_retrieval.description)
        print("工具参数",dataset_retrieval.args)

        content = dataset_retrieval.invoke({"query":"什么是，LLMOPS？"})
        return success_json({"content":content})


    @login_required
    def debug_chat(self, app_id:UUID):
        """根据传入的id +  query 发起调试对话"""
        # 1.提取数据并校验
        req = DebugChatReq()
        if not req.validate():
            return validate_error_json(req.errors)

        #2、调用服务发起会话
        response = self.app_service.debug_chat(app_id, req.query.data, current_user)
        return compact_generate_response(response)

    @login_required
    def delete_debug_conversation(self, app_id:UUID):
        """根据传递的应用id, 清除应用的调试会话记录"""
        self.app_service.delete_debug_conversation(app_id, current_user)
        return success_message("清空应用调试会话记录成功！")






